package pit.com.ordermobilesystem;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.aviary.android.feather.FeatherActivity;
import com.aviary.android.feather.library.Constants;
import com.google.android.gms.internal.he;

import android.content.Context;
import android.content.Intent;
import android.graphics.Point;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Display;

public class CameraActivity extends BaseFragmentActivity{
	private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
	private static final int PROCESS_IMAGE_REQUEST_CODE = 101;
	private Uri fileUri;
	@Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        // create Intent to take a picture and return control to the calling application
	        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

	        fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE,getApplicationContext()); // create a file to save the image
	        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

	        // start the image capture Intent
	        startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
	    }
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) 
	{
		    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
			        if (resultCode == RESULT_OK) {
	
				        	Display display = getWindowManager().getDefaultDisplay();
				        	Point size = new Point();
				        	display.getSize(size);
				        	int width = size.x;
				        	int height=size.y;
				            // User cancelled the image capture
					     Intent newIntent = new Intent( this, FeatherActivity.class );
					     newIntent.setData( fileUri );
					     newIntent.putExtra( Constants.EXTRA_IN_API_KEY_SECRET, "3d266296bab5e98b" );
					     //newIntent.putExtra( Constants.EXTRA_MAX_IMAGE_SIZE, 900 );
					     startActivityForResult( newIntent, PROCESS_IMAGE_REQUEST_CODE );  			                    
			        } else if (resultCode == RESULT_CANCELED) {
			                    finish();
			        	
			        } 
		    }else if(requestCode==PROCESS_IMAGE_REQUEST_CODE){
			    if(resultCode==RESULT_OK){
			                // output image path
			                Uri mImageUri = data.getData();
			                Bundle extra = data.getExtras();
			                if( null != extra ) {
			                        // image has been changed by the user?
			                        boolean changed = extra.getBoolean( Constants.EXTRA_OUT_BITMAP_CHANGED );
			                        setResult(RESULT_OK, data);

			                        
			                 }
			                 finish();
			    }else{
		                    finish();
			    }
			    
		    }


	   
	}
	
	
	public static final int MEDIA_TYPE_IMAGE = 1;
	public static final int MEDIA_TYPE_VIDEO = 2;

	/** Create a file Uri for saving an image or video */
	private static Uri getOutputMediaFileUri(int type,Context context){
	      return Uri.fromFile(getOutputMediaFile(type,context));
	}

	/** Create a File for saving an image or video */
	private static File getOutputMediaFile(int type,Context context){
	    // To be safe, you should check that the SDCard is mounted
	    // using Environment.getExternalStorageState() before doing this.

	    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
	              Environment.DIRECTORY_PICTURES),context.getString(R.string.directory) );
	    // This location works best if you want the created images to be shared
	    // between applications and persist after your app has been uninstalled.

	    // Create the storage directory if it does not exist
	    if (! mediaStorageDir.exists()){
	        if (! mediaStorageDir.mkdirs()){
	           
	            return null;
	        }
	    }

	    // Create a media file name
	    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
	    File mediaFile;
	    if (type == MEDIA_TYPE_IMAGE){
	        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
	        "IMG_"+ timeStamp + ".jpg");
	    } else if(type == MEDIA_TYPE_VIDEO) {
	        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
	        "VID_"+ timeStamp + ".mp4");
	    } else {
	        return null;
	    }

	    return mediaFile;
	}	
}
